/**
 * 
 */
package GUI.main.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import officeserver.log_error.AppointmentException;
import officeserver.log_error.UserException;
import officeserver.report.Appointment;
import officeserver.report.LabWork;
import officeserver.report.Prescription;
import officeserver.report.Treatment;
import officeserver.users.AccessLevel;
import officeserver.users.PersonalInfo;

import GUI.calendar.CalendarPanel;
import GUI.helpers.ButtonType;
import GUI.helpers.PButton;
import GUI.helpers.PTextField;
import GUI.helpers.ProjectConstants;
import GUI.helpers.TextFieldType;
import GUI.main.MainClient;

/**
 * @author mramsey3
 * 
 */
public class AddTreatmentDialog extends JDialog implements ActionListener,
	MouseListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JPanel titlePanel;
    private JPanel buttonPanel;

    private PButton acceptButton;
    private PButton cancelButton;

    private JTabbedPane tabPane;
    private JPanel labWorkPanel;
    private JPanel perscriptionPanel;

    private JPanel northLWPanel;
    private JPanel centerLWPanel;

    private JPanel northLWCenterPanel;
    private JPanel northLWSouthPanel;

    private JPanel centerLWNorthPanel;
    private JTextArea centerLWCenterArea;

    private JPanel patientPanelLW;
    private JLabel patientLabelLW;
    private PTextField patientFieldLW;

    private JPanel doctorPanelLW;
    private JLabel doctorLabelLW;
    private PTextField doctorFieldLW;

    private JPanel nursePanelLW;
    private JLabel nurseLabelLW;
    private JComboBox nurseComboBoxLW;

    private JPanel datePanelLW;
    private JLabel dateLabelLW;
    private PTextField dateFieldLW;

    private JRadioButton bloodRadio;
    private JRadioButton strepRadio;
    private JRadioButton monoRadio;
    private JRadioButton urineRadio;
    private ButtonGroup radioGroup;

    private JPanel northPPanel;
    private JPanel centerPPanel;

    private JPanel patientPPanel;
    private JLabel patientPLabel;
    private PTextField patientPField;

    private JPanel doctorPPanel;
    private JLabel doctorPLabel;
    private PTextField doctorPField;

    private JPanel datePPanel;
    private JLabel datePLabel;
    private PTextField datePField;

    private JPanel drugPPanel;
    private JLabel drugPLabel;
    private PTextField drugPField;

    private JPanel dosagePPanel;
    private JLabel dosagePLabel;
    private PTextField dosagePField;

    private JPanel costPPanel;
    private JLabel costPLabel;
    private PTextField costPField;

    private JPanel centerPNorthPanel;
    private JTextArea centerPCenterArea;

    private static final int WIDTH = 500;
    private static final int HEIGHT = 650;

    private Appointment appointment;
    private ReportDialog rd;

    public AddTreatmentDialog(Appointment appointment, ReportDialog rd) {
	this.appointment = appointment;
	this.rd = rd;
	setLayout(new BorderLayout());
	setSize(new Dimension(WIDTH, HEIGHT));
	final Toolkit toolkit = Toolkit.getDefaultToolkit();
	final Dimension screenSize = toolkit.getScreenSize();
	final int x = (screenSize.width - WIDTH) / 2;
	final int y = (screenSize.height - HEIGHT) / 2;
	setLocation(x, y);
	add(getTitlePanel(), BorderLayout.NORTH);
	add(getTabPane(), BorderLayout.CENTER);
	add(getButtonPanel(), BorderLayout.SOUTH);
    }

    private JPanel getTitlePanel() {
	if (titlePanel == null) {
	    titlePanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.SUBHEADER_COLOR);
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
		    g.drawString("add treatment:", 24, getHeight() - 8);
		}
	    };
	    titlePanel.setPreferredSize(new Dimension(WIDTH,
		    ProjectConstants.HEADER_HEIGHT));
	    titlePanel.addMouseListener(this);
	}
	return titlePanel;
    }

    private JTabbedPane getTabPane() {
	if (tabPane == null) {
	    tabPane = new JTabbedPane();
	    tabPane.addTab("lab work", getLabWorkPanel());
	    tabPane.addTab("perscription", getPerscriptionPanel());
	    tabPane.addMouseListener(this);
	}
	return tabPane;
    }

    private JPanel getLabWorkPanel() {
	if (labWorkPanel == null) {
	    labWorkPanel = new JPanel();

	    labWorkPanel.setLayout(new BorderLayout());

	    labWorkPanel.add(getNorthLWPanel(), BorderLayout.NORTH);
	    labWorkPanel.add(getCenterLWPanel(), BorderLayout.CENTER);
	    labWorkPanel.addMouseListener(this);
	}
	return labWorkPanel;
    }

    private JPanel getNorthLWPanel() {
	if (northLWPanel == null) {
	    northLWPanel = new JPanel();

	    northLWPanel.setLayout(new BorderLayout());
	    northLWPanel.setBorder(new EmptyBorder(0, 10, 0, 10));

	    northLWPanel.add(getNorthLWCenterPanel(), BorderLayout.CENTER);
	    northLWPanel.add(getNorthLWSouthPanel(), BorderLayout.SOUTH);
	    northLWPanel.addMouseListener(this);
	}
	return northLWPanel;
    }

    private JPanel getNorthLWCenterPanel() {
	if (northLWCenterPanel == null) {
	    northLWCenterPanel = new JPanel();

	    northLWCenterPanel.setLayout(new BoxLayout(northLWCenterPanel,
		    BoxLayout.PAGE_AXIS));

	    northLWCenterPanel.add(getPatientPanelLW());
	    northLWCenterPanel.add(getDoctorPanelLW());
	    northLWCenterPanel.add(getNursePanelLW());
	    northLWCenterPanel.add(getDatePanelLW());
	    northLWCenterPanel.addMouseListener(this);
	}
	return northLWCenterPanel;
    }

    private JPanel getPatientPanelLW() {
	if (patientPanelLW == null) {
	    patientPanelLW = new JPanel();

	    patientPanelLW.setBorder(new EmptyBorder(10, 10, 0, 10));
	    patientPanelLW.setLayout(new BorderLayout());

	    patientPanelLW.add(getPatientLabelLW(), BorderLayout.WEST);
	    patientPanelLW.add(getPatientFieldLW(), BorderLayout.CENTER);
	    patientPanelLW.addMouseListener(this);
	}
	return patientPanelLW;
    }

    private JLabel getPatientLabelLW() {
	if (patientLabelLW == null) {
	    patientLabelLW = new JLabel(ProjectConstants.CELLPAD + "Patient:");
	    patientLabelLW.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH, 25));
	    patientLabelLW.addMouseListener(this);
	}
	return patientLabelLW;
    }

    private PTextField getPatientFieldLW() {
	if (patientFieldLW == null) {
	    try {
		MainClient.getConnection().userSearch(
			appointment.getPatientID(), AccessLevel.PATIENT);
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    PersonalInfo pInfo = null;
	    try {
		pInfo = MainClient.getConnection().getSearchPersonalInfo();
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    patientFieldLW = new PTextField(pInfo.getFirstName() + " "
		    + pInfo.getLastName(), TextFieldType.LONG);
	    patientFieldLW.setEditable(false);
	    patientFieldLW.addMouseListener(this);
	}
	return patientFieldLW;
    }

    private JPanel getDoctorPanelLW() {
	if (doctorPanelLW == null) {
	    doctorPanelLW = new JPanel();

	    doctorPanelLW.setBorder(new EmptyBorder(10, 10, 0, 10));
	    doctorPanelLW.setLayout(new BorderLayout());

	    doctorPanelLW.add(getDoctorLabelLW(), BorderLayout.WEST);
	    doctorPanelLW.add(getDoctorFieldLW(), BorderLayout.CENTER);
	    doctorPanelLW.addMouseListener(this);
	}
	return doctorPanelLW;
    }

    private JLabel getDoctorLabelLW() {
	if (doctorLabelLW == null) {
	    doctorLabelLW = new JLabel(ProjectConstants.CELLPAD + "Doctor:");
	    doctorLabelLW.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH, 25));
	    doctorLabelLW.addMouseListener(this);
	}
	return doctorLabelLW;
    }

    private PTextField getDoctorFieldLW() {
	if (doctorFieldLW == null) {
	    try {
		MainClient.getConnection().userSearch(
			appointment.getDoctorID(), AccessLevel.DOCTOR);
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    PersonalInfo pInfo = null;
	    try {
		pInfo = MainClient.getConnection().getSearchPersonalInfo();
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    doctorFieldLW = new PTextField(pInfo.getFirstName() + " "
		    + pInfo.getLastName(), TextFieldType.LONG);
	    doctorFieldLW.addMouseListener(this);
	    doctorFieldLW.setEditable(false);
	}
	return doctorFieldLW;
    }

    private JPanel getNursePanelLW() {
	if (nursePanelLW == null) {
	    nursePanelLW = new JPanel();

	    nursePanelLW.setBorder(new EmptyBorder(10, 10, 0, 10));
	    nursePanelLW.setLayout(new BorderLayout());

	    nursePanelLW.add(getNurseLabelLW(), BorderLayout.WEST);
	    nursePanelLW.add(getNurseComboBoxLW(), BorderLayout.CENTER);
	    nursePanelLW.addMouseListener(this);
	}
	return nursePanelLW;
    }

    private JLabel getNurseLabelLW() {
	if (nurseLabelLW == null) {
	    nurseLabelLW = new JLabel(ProjectConstants.CELLPAD + "Nurse:");
	    nurseLabelLW.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH, 25));
	    nurseLabelLW.addMouseListener(this);
	}
	return nurseLabelLW;
    }

    private JComboBox getNurseComboBoxLW() {
	if (nurseComboBoxLW == null) {
	    ArrayList<PersonalInfo> nurses = new ArrayList<PersonalInfo>();
	    try {
		nurses = MainClient.getConnection().getNurses();
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    String[] names = new String[nurses.size()];
	    for (int i = 0; i < nurses.size(); i++) {
		names[i] = nurses.get(i).getFirstName() + " "
			+ nurses.get(i).getLastName();
	    }
	    nurseComboBoxLW = new JComboBox(names);
	    nurseComboBoxLW.setPreferredSize(new Dimension(250, 25));
	    nurseComboBoxLW.setSelectedIndex(0);
	    nurseComboBoxLW.addMouseListener(this);
	}
	return nurseComboBoxLW;
    }

    private JPanel getDatePanelLW() {
	if (datePanelLW == null) {
	    datePanelLW = new JPanel();

	    datePanelLW.setBorder(new EmptyBorder(10, 10, 10, 10));
	    datePanelLW.setLayout(new BorderLayout());

	    datePanelLW.add(getDateLabelLW(), BorderLayout.WEST);
	    datePanelLW.add(getDateFieldLW(), BorderLayout.CENTER);
	    datePanelLW.addMouseListener(this);
	}
	return datePanelLW;
    }

    private JLabel getDateLabelLW() {
	if (dateLabelLW == null) {
	    dateLabelLW = new JLabel(ProjectConstants.CELLPAD + "Date:");
	    dateLabelLW.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH, 25));
	    dateLabelLW.addMouseListener(this);
	}
	return dateLabelLW;
    }

    private PTextField getDateFieldLW() {
	if (dateFieldLW == null) {
	    appointment.getDay();
	    GregorianCalendar gc = new GregorianCalendar(2011, 0, 1);
	    gc.roll(Calendar.DAY_OF_YEAR, appointment.getDay());
	    StringBuilder builder = new StringBuilder();
	    builder.append(CalendarPanel.getMonthMap().get(
		    gc.get(Calendar.MONTH)));
	    builder.append(" ");
	    builder.append(gc.get(Calendar.DAY_OF_MONTH));
	    builder.append(", ");
	    builder.append(gc.get(Calendar.YEAR));
	    dateFieldLW = new PTextField(builder.toString(), TextFieldType.LONG);
	    dateFieldLW.setEditable(false);
	    dateFieldLW.addMouseListener(this);
	}
	return dateFieldLW;
    }

    private JPanel getNorthLWSouthPanel() {
	if (northLWSouthPanel == null) {
	    northLWSouthPanel = new JPanel();

	    northLWSouthPanel.setLayout(new GridLayout(2, 2));
	    northLWSouthPanel.setBorder(BorderFactory
		    .createTitledBorder("Lab Work Type:"));

	    radioGroup = new ButtonGroup();

	    bloodRadio = new JRadioButton("Blood Work");
	    radioGroup.add(bloodRadio);
	    JPanel temp1 = new JPanel();
	    temp1.setBorder(new EmptyBorder(0, 5, 0, 5));
	    temp1.add(bloodRadio);
	    northLWSouthPanel.add(temp1);

	    strepRadio = new JRadioButton("Strep Test");
	    radioGroup.add(strepRadio);
	    JPanel temp2 = new JPanel();
	    temp2.setBorder(new EmptyBorder(0, 5, 0, 5));
	    temp2.add(strepRadio);
	    northLWSouthPanel.add(temp2);

	    monoRadio = new JRadioButton("Mono Test");
	    radioGroup.add(monoRadio);
	    JPanel temp3 = new JPanel();
	    temp3.setBorder(new EmptyBorder(0, 5, 5, 5));
	    temp3.add(monoRadio);
	    northLWSouthPanel.add(temp3);

	    urineRadio = new JRadioButton("Urine Test");
	    radioGroup.add(urineRadio);
	    JPanel temp4 = new JPanel();
	    temp4.setBorder(new EmptyBorder(0, 5, 5, 5));
	    temp4.add(urineRadio);
	    northLWSouthPanel.add(temp4);
	    northLWSouthPanel.addMouseListener(this);
	}
	return northLWSouthPanel;
    }

    private JPanel getCenterLWPanel() {
	if (centerLWPanel == null) {
	    centerLWPanel = new JPanel();

	    centerLWPanel.setLayout(new BorderLayout());
	    centerLWPanel.setBorder(new EmptyBorder(10, 10, 10, 10));

	    centerLWPanel.add(getCenterLWNorthPanel(), BorderLayout.NORTH);
	    centerLWPanel.add(getCenterLWCenterArea(), BorderLayout.CENTER);
	    centerLWPanel.addMouseListener(this);
	}
	return centerLWPanel;
    }

    private JPanel getCenterLWNorthPanel() {
	if (centerLWNorthPanel == null) {
	    centerLWNorthPanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.SUBHEADER_COLOR);
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
		    g.drawString("instructions:", 24, getHeight() - 8);
		}
	    };
	    centerLWNorthPanel.setPreferredSize(new Dimension(WIDTH, 50));
	    centerLWNorthPanel.addMouseListener(this);
	}
	return centerLWNorthPanel;
    }

    private JTextArea getCenterLWCenterArea() {

	if (centerLWCenterArea == null) {
	    centerLWCenterArea = new JTextArea("double-click to edit");
	    centerLWCenterArea.setPreferredSize(new Dimension(WIDTH, 150));
	    centerLWCenterArea.setBorder(new EmptyBorder(10, 10, 10, 10));
	    centerLWCenterArea.setEditable(false);
	    centerLWCenterArea.addMouseListener(this);
	}
	return centerLWCenterArea;
    }

    private JPanel getPerscriptionPanel() {
	if (perscriptionPanel == null) {
	    perscriptionPanel = new JPanel();
	    perscriptionPanel.setLayout(new BorderLayout());

	    perscriptionPanel.add(getNorthPPanel(), BorderLayout.NORTH);
	    perscriptionPanel.add(getCenterPPanel(), BorderLayout.CENTER);
	    perscriptionPanel.addMouseListener(this);
	}
	return perscriptionPanel;
    }

    private JPanel getNorthPPanel() {
	if (northPPanel == null) {
	    northPPanel = new JPanel();

	    northPPanel.setLayout(new BoxLayout(northPPanel,
		    BoxLayout.PAGE_AXIS));
	    northPPanel.setBorder(new EmptyBorder(0, 10, 0, 10));

	    northPPanel.add(getPatientPPanel());
	    northPPanel.add(getDoctorPPanel());
	    northPPanel.add(getDatePPanel());
	    northPPanel.add(getDrugPPanel());
	    northPPanel.add(getDosagePPanel());
	    northPPanel.add(getCostPPanel());
	    northPPanel.addMouseListener(this);
	}
	return northPPanel;
    }

    private JPanel getPatientPPanel() {
	if (patientPPanel == null) {
	    patientPPanel = new JPanel();

	    patientPPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
	    patientPPanel.setLayout(new BorderLayout());

	    patientPPanel.add(getPatientPLabel(), BorderLayout.WEST);
	    patientPPanel.add(getPatientPField(), BorderLayout.CENTER);
	    patientPPanel.addMouseListener(this);
	}
	return patientPPanel;
    }

    private JLabel getPatientPLabel() {
	if (patientPLabel == null) {
	    patientPLabel = new JLabel(ProjectConstants.CELLPAD + "Patient:");
	    patientPLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH, 25));
	    patientPLabel.addMouseListener(this);
	}
	return patientPLabel;
    }

    private PTextField getPatientPField() {
	if (patientPField == null) {
	    try {
		MainClient.getConnection().userSearch(
			appointment.getPatientID(), AccessLevel.PATIENT);
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    PersonalInfo pInfo = null;
	    try {
		pInfo = MainClient.getConnection().getSearchPersonalInfo();
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    patientPField = new PTextField(pInfo.getFirstName() + " "
		    + pInfo.getLastName(), TextFieldType.LONG);
	    patientPField.setEditable(false);
	    patientPField.addMouseListener(this);
	}
	return patientPField;
    }

    private JPanel getDoctorPPanel() {
	if (doctorPPanel == null) {
	    doctorPPanel = new JPanel();

	    doctorPPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
	    doctorPPanel.setLayout(new BorderLayout());

	    doctorPPanel.add(getDoctorPLabel(), BorderLayout.WEST);
	    doctorPPanel.add(getDoctorPField(), BorderLayout.CENTER);
	    doctorPPanel.addMouseListener(this);
	}
	return doctorPPanel;
    }

    private JLabel getDoctorPLabel() {
	if (doctorPLabel == null) {
	    doctorPLabel = new JLabel(ProjectConstants.CELLPAD + "Doctor:");
	    doctorPLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH, 25));
	    doctorPLabel.addMouseListener(this);
	}
	return doctorPLabel;
    }

    private PTextField getDoctorPField() {
	if (doctorPField == null) {
	    try {
		MainClient.getConnection().userSearch(
			appointment.getDoctorID(), AccessLevel.DOCTOR);
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    PersonalInfo pInfo = null;
	    try {
		pInfo = MainClient.getConnection().getSearchPersonalInfo();
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    doctorPField = new PTextField(pInfo.getFirstName() + " "
		    + pInfo.getLastName(), TextFieldType.LONG);
	    doctorPField.addMouseListener(this);
	    doctorPField.setEditable(false);
	}
	return doctorPField;
    }

    private JPanel getDatePPanel() {
	if (datePPanel == null) {
	    datePPanel = new JPanel();

	    datePPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
	    datePPanel.setLayout(new BorderLayout());

	    datePPanel.add(getDatePLabel(), BorderLayout.WEST);
	    datePPanel.add(getDatePField(), BorderLayout.CENTER);
	    datePPanel.addMouseListener(this);
	}
	return doctorPPanel;
    }

    private JLabel getDatePLabel() {
	if (datePLabel == null) {
	    datePLabel = new JLabel(ProjectConstants.CELLPAD + "Date:");
	    datePLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH, 25));
	    datePLabel.addMouseListener(this);
	}
	return datePLabel;
    }

    private PTextField getDatePField() {
	if (datePField == null) {
	    appointment.getDay();
	    GregorianCalendar gc = new GregorianCalendar(2011, 0, 1);
	    gc.roll(Calendar.DAY_OF_YEAR, appointment.getDay());
	    StringBuilder builder = new StringBuilder();
	    builder.append(CalendarPanel.getMonthMap().get(
		    gc.get(Calendar.MONTH)));
	    builder.append(" ");
	    builder.append(gc.get(Calendar.DAY_OF_MONTH));
	    builder.append(", ");
	    builder.append(gc.get(Calendar.YEAR));
	    datePField = new PTextField(builder.toString(), TextFieldType.LONG);
	    datePField.setEditable(false);
	    datePField.addMouseListener(this);
	}
	return datePField;
    }

    private JPanel getDrugPPanel() {
	if (drugPPanel == null) {
	    drugPPanel = new JPanel();

	    drugPPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
	    drugPPanel.setLayout(new BorderLayout());

	    drugPPanel.add(getDrugPLabel(), BorderLayout.WEST);
	    drugPPanel.add(getDrugPField(), BorderLayout.CENTER);
	    drugPPanel.addMouseListener(this);
	}
	return drugPPanel;
    }

    private JLabel getDrugPLabel() {
	if (drugPLabel == null) {
	    drugPLabel = new JLabel(ProjectConstants.CELLPAD + "Drug:");
	    drugPLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH, 25));
	    drugPLabel.addMouseListener(this);
	}
	return drugPLabel;
    }

    private PTextField getDrugPField() {
	if (drugPField == null) {
	    drugPField = new PTextField("drug", TextFieldType.LONG);
	    drugPField.addMouseListener(this);
	}
	return drugPField;
    }

    private JPanel getDosagePPanel() {
	if (dosagePPanel == null) {
	    dosagePPanel = new JPanel();

	    dosagePPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
	    dosagePPanel.setLayout(new BorderLayout());

	    dosagePPanel.add(getDosagePLabel(), BorderLayout.WEST);
	    dosagePPanel.add(getDosagePField(), BorderLayout.CENTER);
	    dosagePPanel.addMouseListener(this);
	}
	return dosagePPanel;
    }

    private JLabel getDosagePLabel() {
	if (dosagePLabel == null) {
	    dosagePLabel = new JLabel(ProjectConstants.CELLPAD + "Dosage:");
	    dosagePLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH, 25));
	    dosagePLabel.addMouseListener(this);
	}
	return dosagePLabel;
    }

    private PTextField getDosagePField() {
	if (dosagePField == null) {
	    dosagePField = new PTextField("dosage", TextFieldType.LONG);
	    dosagePField.addMouseListener(this);
	}
	return dosagePField;
    }

    private JPanel getCostPPanel() {
	if (costPPanel == null) {
	    costPPanel = new JPanel();

	    costPPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
	    costPPanel.setLayout(new BorderLayout());

	    costPPanel.add(getCostPLabel(), BorderLayout.WEST);
	    costPPanel.add(getCostPField(), BorderLayout.CENTER);
	    costPPanel.addMouseListener(this);
	}
	return costPPanel;
    }

    private JLabel getCostPLabel() {
	if (costPLabel == null) {
	    costPLabel = new JLabel(ProjectConstants.CELLPAD + "Cost:");
	    costPLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH, 25));
	    costPLabel.addMouseListener(this);
	}
	return costPLabel;
    }

    private PTextField getCostPField() {
	if (costPField == null) {
	    costPField = new PTextField("cost", TextFieldType.LONG);
	    costPField.addMouseListener(this);
	}
	return costPField;
    }

    private JPanel getCenterPPanel() {
	if (centerPPanel == null) {
	    centerPPanel = new JPanel();
	    centerPPanel.setLayout(new BorderLayout());
	    centerPPanel.setBorder(new EmptyBorder(89, 10, 10, 10));

	    centerPPanel.add(getCenterPNorthPanel(), BorderLayout.NORTH);
	    centerPPanel.add(getCenterPCenterArea(), BorderLayout.CENTER);
	    centerPPanel.addMouseListener(this);
	}
	return centerPPanel;
    }

    private JPanel getCenterPNorthPanel() {
	if (centerPNorthPanel == null) {
	    centerPNorthPanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.SUBHEADER_COLOR);
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
		    g.drawString("instructions:", 24, getHeight() - 8);
		}
	    };
	    centerPNorthPanel.setPreferredSize(new Dimension(WIDTH, 50));
	    centerPNorthPanel.addMouseListener(this);
	}
	return centerPNorthPanel;
    }

    private JTextArea getCenterPCenterArea() {
	if (centerPCenterArea == null) {
	    centerPCenterArea = new JTextArea("double-click to edit");
	    centerPCenterArea.setPreferredSize(new Dimension(WIDTH, 150));
	    centerPCenterArea.setBorder(new EmptyBorder(10, 10, 10, 10));
	    centerPCenterArea.setEditable(false);
	    centerPCenterArea.addMouseListener(this);
	}
	return centerPCenterArea;
    }

    private JPanel getButtonPanel() {
	if (buttonPanel == null) {
	    buttonPanel = new JPanel();
	    buttonPanel.setBorder(new EmptyBorder(20, 10, 10, 10));
	    buttonPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.gridx = 0;
	    buttonPanel.add(getAcceptButton(), gbc1);
	    GridBagConstraints gbc2 = new GridBagConstraints();
	    gbc2.gridx = 1;
	    buttonPanel.add(Box.createHorizontalStrut(25), gbc2);
	    GridBagConstraints gbc3 = new GridBagConstraints();
	    gbc3.gridx = 2;
	    buttonPanel.add(getCancelButton(), gbc3);
	    buttonPanel.addMouseListener(this);
	}
	return buttonPanel;
    }

    private PButton getAcceptButton() {
	if (acceptButton == null) {
	    acceptButton = new PButton("accept", ButtonType.MEDIUM);
	    acceptButton.addActionListener(this);
	}
	return acceptButton;
    }

    private PButton getCancelButton() {
	if (cancelButton == null) {
	    cancelButton = new PButton("cancel", ButtonType.MEDIUM);
	    cancelButton.addActionListener(this);
	}
	return cancelButton;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
	if (e.getSource() == getCenterLWCenterArea()) {
	    if (e.getClickCount() == 2) {
		getCenterLWCenterArea().setEditable(true);
		getCenterLWCenterArea().getCaret().setVisible(true);
	    }
	} else if (e.getSource() == getCenterPCenterArea()) {
	    if (e.getClickCount() == 2) {
		getCenterPCenterArea().setEditable(true);
	    }
	} else {
	    getCenterLWCenterArea().setEditable(false);
	    getCenterPCenterArea().setEditable(false);
	}

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
	// TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
	// TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
	// TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
	// TODO Auto-generated method stub

    }

    @Override
    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == getCancelButton()) {
	    setVisible(false);
	}
	if (e.getSource() == getAcceptButton()) {
	    Treatment toAdd = null;
	    if (tabPane.getSelectedComponent() == labWorkPanel) {
		Double cost = null;
		String type = null;
		if (bloodRadio.isSelected()) {
		    cost = ProjectConstants.BLOODWORK_COST;
		    type = "Blood Test";
		    toAdd = new LabWork(getDoctorFieldLW().getText(),
			    getPatientFieldLW().getText(), getDateFieldLW()
				    .getText(), getCenterLWCenterArea()
				    .getText(), cost,
			    (String) getNurseComboBoxLW().getSelectedItem(),
			    type);
		    ArrayList<Treatment> treatments = appointment.getReport()
			    .getTreatments();
		    if (treatments == null)
			treatments = new ArrayList<Treatment>();
		    treatments.add(toAdd);
		    appointment.getReport().setTreatments(treatments);
		    appointment.getReport().setVitalSigns(null);
		    appointment.getReport().setSymptoms(null);
		    try {
			MainClient.getConnection()
				.userSearch(appointment.getPatientID(),
					AccessLevel.PATIENT);
			try {
			    MainClient.getConnection()
				    .updateSearchUserAppointment(appointment);
			    rd.setAppointment(appointment);
			    rd.refreshPanel();
			    this.setVisible(false);
			} catch (Exception e1) {
			    // TODO Auto-generated catch block
			    e1.printStackTrace();
			}
		    } catch (UserException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		    }
		} else if (strepRadio.isSelected()) {
		    cost = ProjectConstants.STREPTEST_COST;
		    type = "Radio Test";
		    toAdd = new LabWork(getDoctorFieldLW().getText(),
			    getPatientFieldLW().getText(), getDateFieldLW()
				    .getText(), getCenterLWCenterArea()
				    .getText(), cost,
			    (String) getNurseComboBoxLW().getSelectedItem(),
			    type);
		    ArrayList<Treatment> treatments = appointment.getReport()
			    .getTreatments();
		    if (treatments == null)
			treatments = new ArrayList<Treatment>();
		    treatments.add(toAdd);
		    appointment.getReport().setTreatments(treatments);
		    appointment.getReport().setVitalSigns(null);
		    appointment.getReport().setSymptoms(null);
		    try {
			MainClient.getConnection()
				.userSearch(appointment.getPatientID(),
					AccessLevel.PATIENT);
			try {
			    MainClient.getConnection()
				    .updateSearchUserAppointment(appointment);
			    rd.setAppointment(appointment);
			    rd.refreshPanel();
			    this.setVisible(false);
			} catch (Exception e1) {
			    // TODO Auto-generated catch block
			    e1.printStackTrace();
			}
		    } catch (UserException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		    }
		} else if (monoRadio.isSelected()) {
		    cost = ProjectConstants.MONOTEST_COST;
		    type = "Mono Test";
		    toAdd = new LabWork(getDoctorFieldLW().getText(),
			    getPatientFieldLW().getText(), getDateFieldLW()
				    .getText(), getCenterLWCenterArea()
				    .getText(), cost,
			    (String) getNurseComboBoxLW().getSelectedItem(),
			    type);
		    ArrayList<Treatment> treatments = appointment.getReport()
			    .getTreatments();
		    if (treatments == null)
			treatments = new ArrayList<Treatment>();
		    treatments.add(toAdd);
		    appointment.getReport().setTreatments(treatments);
		    appointment.getReport().setVitalSigns(null);
		    appointment.getReport().setSymptoms(null);
		    try {
			MainClient.getConnection()
				.userSearch(appointment.getPatientID(),
					AccessLevel.PATIENT);
			try {
			    MainClient.getConnection()
				    .updateSearchUserAppointment(appointment);
			    rd.setAppointment(appointment);
			    rd.refreshPanel();
			    this.setVisible(false);
			} catch (Exception e1) {
			    // TODO Auto-generated catch block
			    e1.printStackTrace();
			}
		    } catch (UserException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		    }
		} else if (urineRadio.isSelected()) {
		    cost = ProjectConstants.URINETEST_COST;
		    type = "Urine Test";
		    toAdd = new LabWork(getDoctorFieldLW().getText(),
			    getPatientFieldLW().getText(), getDateFieldLW()
				    .getText(), getCenterLWCenterArea()
				    .getText(), cost,
			    (String) getNurseComboBoxLW().getSelectedItem(),
			    type);
		    ArrayList<Treatment> treatments = appointment.getReport()
			    .getTreatments();
		    if (treatments == null)
			treatments = new ArrayList<Treatment>();
		    treatments.add(toAdd);
		    appointment.getReport().setTreatments(treatments);
		    appointment.getReport().setVitalSigns(null);
		    appointment.getReport().setSymptoms(null);
		    try {
			MainClient.getConnection()
				.userSearch(appointment.getPatientID(),
					AccessLevel.PATIENT);
			try {
			    MainClient.getConnection()
				    .updateSearchUserAppointment(appointment);
			    rd.setAppointment(appointment);
			    rd.refreshPanel();
			    this.setVisible(false);
			} catch (Exception e1) {
			    // TODO Auto-generated catch block
			    e1.printStackTrace();
			}
		    } catch (UserException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		    }
		} else {
		    JOptionPane.showMessageDialog(this,
			    "Please make a selection.");
		}
	    } else {
		try {
		    Integer test1 = Integer.parseInt(getDosagePField()
			    .getText());
		    if (test1 < 0) {
			JOptionPane.showMessageDialog(this,
				"Please enter a dosage greater than 0.");
		    } else {
			try {
			    Double test = Double.parseDouble(getCostPField()
				    .getText());
			    if (test < 0) {
				JOptionPane.showMessageDialog(this,
					"Please enter a cost greater than $0.");
			    } else {
				toAdd = new Prescription(getDoctorFieldLW()
					.getText(), getPatientFieldLW()
					.getText(), getDateFieldLW().getText(),
					getCenterPCenterArea().getText(),
					Double.parseDouble(getCostPField()
						.getText()),
					Integer.parseInt(getDosagePField()
						.getText()), getDrugPField()
						.getText());
			    }
			} catch (NumberFormatException e1) {
			    JOptionPane.showMessageDialog(this,
				    "Please enter a numeric cost.");
			}
		    }
		} catch (NumberFormatException e2) {
		    JOptionPane.showMessageDialog(this,
			    "Please enter a numeric dosage.");
		}
		ArrayList<Treatment> treatments = appointment.getReport()
			.getTreatments();
		if (treatments == null)
		    treatments = new ArrayList<Treatment>();
		treatments.add(toAdd);
		appointment.getReport().setTreatments(treatments);
		appointment.getReport().setVitalSigns(null);
		appointment.getReport().setSymptoms(null);
		try {
		    MainClient.getConnection().userSearch(
			    appointment.getPatientID(), AccessLevel.PATIENT);
		    try {
			MainClient.getConnection().updateSearchUserAppointment(
				appointment);
			rd.setAppointment(appointment);
			rd.refreshPanel();
			this.setVisible(false);
		    } catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		    }
		} catch (UserException e1) {
		    // TODO Auto-generated catch block
		    e1.printStackTrace();
		}
	    }
	}

    }

}
